﻿using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class WhBatchDal
    {
        private readonly AppDbContext _dbContext;

        public WhBatchDal(AppDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="data">实体</param>
        /// <returns>结果</returns>
        public async Task<WhBatch> CreateAsync(WhBatch data)
        {
            // 新增实体到数据库集
            _dbContext.WhBatch.Add(data);
            // 保存更改
            await _dbContext.SaveChangesAsync();
            return data;

        }

        /// <summary>
        /// 根据ID查询
        /// </summary>
        /// <param name="Id">ID</param>
        /// <returns>用户列表</returns>
        public async Task<WhBatch> GetByIdAsync(int Id)
        {
            // 根据ID查询
            return await _dbContext.WhBatch.FirstOrDefaultAsync(g => g.BatchId == Id);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns>用户列表</returns>
        public async Task<List<WhBatch>> GetAllAsync()
        {
            // 查询所有并转换为列表
            return await _dbContext.WhBatch.ToListAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="date">包含更新信息</param>
        public async Task<int> UpdateAsync(WhBatch date)
        {
            // 标记实体为修改状态
            _dbContext.WhBatch.Update(date);
            // 保存更改
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="Id">ID</param>
        public async Task<int> DeleteAsync(int Id)
        {
            // 先查询实体
            var data = await GetByIdAsync(Id);
            if (data != null)
            {
                // 移除实体
                _dbContext.WhBatch.Remove(data);
                // 保存更改
                return await _dbContext.SaveChangesAsync();
            }
            return 0;
        }
    }
}
